package com.blrs.system.service.impl;

import java.util.List;

import com.blrs.common.exception.ServiceException;
import com.blrs.common.utils.DateUtils;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.StringUtils;
import com.blrs.common.utils.uuid.IdUtils;
import com.blrs.system.domain.SysUpload;
import com.blrs.system.mapper.SysUploadFileMapper;
import com.blrs.system.mapper.SysUploadMapper;
import com.blrs.system.service.ISysUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 附件类型Service业务层处理
 * 
 * @author yuxiang.chen
 * @date 2023-07-20
 */
@Service
public class SysUploadServiceImpl implements ISysUploadService
{
    @Autowired
    private SysUploadMapper sysUploadMapper;
    @Autowired
    private SysUploadFileMapper sysUploadFileMapper;

    /**
     * 查询附件类型
     * 
     * @param Id 附件类型主键
     * @return 附件类型
     */
    @Override
    public SysUpload selectSysUploadById(String Id)
    {
        return sysUploadMapper.selectSysUploadById(Id);
    }

    /**
     * 查询附件类型列表
     * 
     * @param sysUpload 附件类型
     * @return 附件类型
     */
    @Override
    public List<SysUpload> selectSysUploadList(SysUpload sysUpload)
    {
        return sysUploadMapper.selectSysUploadList(sysUpload);
    }

    /**
     * 新增附件类型
     * 
     * @param sysUpload 附件类型
     * @return 结果
     */
    @Override
    public int insertSysUpload(SysUpload sysUpload)
    {
        if(sysUpload.getBusinessCode()==null){
            throw new ServiceException("类型编号不能为空！");
        }
        if(sysUpload.getName()==null){
            throw new ServiceException("类型名称不能为空！");
        }
        if(sysUpload.getType()==null){
            throw new ServiceException("上传文件类型不能为空！");
        }
        if(!sysUpload.getType().startsWith(".")){
            throw new ServiceException("上传文件类型格式错误！");
        }else{
            String[] types=sysUpload.getType().split(",");
            for (String type : types) {
                if(!type.startsWith(".")){
                    throw new ServiceException("上传文件类型格式错误！");
                }
            }
        }
        Long code=sysUploadMapper.selectSysUploadByCount(sysUpload.getBusinessCode());
        if(code>0){
            throw new ServiceException("类型编号已存在！");
        }

        sysUpload.setOperateId(String.valueOf(SecurityUtils.getUserId()));
        sysUpload.setId(IdUtils.fastUUID());
        sysUpload.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        sysUpload.setUpdateTime(DateUtils.getNowDate());
        sysUpload.setCreateTime(DateUtils.getNowDate());
        sysUpload.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        return sysUploadMapper.insertSysUpload(sysUpload);
    }

    /**
     * 修改附件类型
     * 
     * @param sysUpload 附件类型
     * @return 结果
     */
    @Override
    public int updateSysUpload(SysUpload sysUpload)
    {
        sysUpload.setUpdateTime(DateUtils.getNowDate());
        return sysUploadMapper.updateSysUpload(sysUpload);
    }

    /**
     * 批量删除附件类型
     * 
     * @param Ids 需要删除的附件类型主键
     * @return 结果
     */
    @Override
    public int deleteSysUploadByIds(String[] Ids)
    {
        return sysUploadMapper.deleteSysUploadByIds(Ids);
    }

    /**
     * 删除附件类型信息
     * 
     * @param Id 附件类型主键
     * @return 结果
     */
    @Override
    public int deleteSysUploadById(String Id)
    {
        SysUpload sysUpload=this.selectSysUploadById(Id);
        if(StringUtils.isNotNull(sysUpload)){
            Long count =sysUploadFileMapper.getCount(sysUpload.getBusinessCode());
            if(count>0){
                throw new ServiceException("请先删除附件信息！");
            }else{
                return sysUploadMapper.deleteSysUploadById(Id);
            }
        }

        return 0;
    }
    /**
     * 获取文件类型
     * @param businessCode 类型编号
     * @return
     */
    @Override
    public SysUpload selectSysUploadByCode(String businessCode) {
        return sysUploadMapper.selectSysUploadByCode(businessCode);
    }

    @Override
    public Long selectSysUploadByCount(String businessCode) {
        return sysUploadMapper.selectSysUploadByCount(businessCode);
    }

    public static void main(String[] args) {
        String str=".png,.jpg,.xml";
        String[] s=str.split(",");
        for (String s1 : s) {
            System.out.println(s1);
        }
    }
}
